from qiniu import *
import uuid

__ak = 'Ga0hcTdMRgmToFRm-n91BdiHrs9HPkm1K8wZTC_A'
__sk = '6RVUr25ykVmqjfENvBnMA_52YK-gX2Z8U6enfy7M'
__bucket_name = 'zyfx'

def upload(img):
    _img = img.read()
    key = str(uuid.uuid1()).replace('-', '') + '.png'
    access_key = 'PzpqWHOwnP_oVBWR3FtbzCUFMzl14N1aL7QK4eqO'
    secret_key = 'XqE5OQmvlNynrqcNbw1_oSYQgoqcpMdZvraircX0'
    # q = Auth(access_key, secret_key)
    q = Auth(__ak, __sk)
    # bucket_name = 'dddd-files'
    bucket_name = __bucket_name
    token = q.upload_token(bucket_name, key, 3600, )
    ret, info = put_data(token, key, _img)
    url = 'https://qiniu.zouyunjs.com/{}'.format(key)

    return url


def get_upload_token():
    access_key = 'PzpqWHOwnP_oVBWR3FtbzCUFMzl14N1aL7QK4eqO'
    secret_key = 'XqE5OQmvlNynrqcNbw1_oSYQgoqcpMdZvraircX0'
    # q = Auth(access_key, secret_key)
    q = Auth(__ak, __sk)
    # bucket_name = 'dddd-files'
    bucket_name = __bucket_name

    token = q.upload_token(bucket_name, None, 3600, )
    return_dict = {"token": token}
    return return_dict


def get_image(file_path):
    access_key = 'PzpqWHOwnP_oVBWR3FtbzCUFMzl14N1aL7QK4eqO'
    secret_key = 'XqE5OQmvlNynrqcNbw1_oSYQgoqcpMdZvraircX0'
    # q = Auth(access_key, secret_key)
    q = Auth(__ak, __sk)
    # 要转码的文件所在的空间和文件名。
    # bucket = 'dddd-files'
    bucket = __bucket_name

    key = file_path
    # 转码是使用的队列名称。
    pipeline = 'lucky_queue'
    # 要进行转码的转码操作。
    fops = 'vframe/jpg/offset/1/w/178/h/178'
    # 可以对转码后的文件进行使用saveas参数自定义命名，当然也可以不指定文件会默认命名并保存在当前空间
    pfop = PersistentFop(q, bucket, pipeline)
    ops = []
    ops.append(fops)
    ret, info = pfop.execute(key, ops, 1)
    return info


def get_video_image(file_path):
    _path = file_path
    _image_path = _path + "?vframe/jpg/offset/1/w/178/h/178"
    return _image_path


# if __name__ == '__main__':
#     _path = get_image('test.mp4').text_body
#     _img = eval(_path)['persistentId']
#     url = 'http://api.qiniu.com/status/get/prefop?id=' + _img
#     res = requests.get(url)
#     res.encoding = 'utf-8'
#     html_response = str(res.content, encoding="utf-8")
#     print(html_response)
